package lapsnake.model.stmt;

import lapsnake.model.*;
import lapsnake.model.data.*;
import lapsnake.py.ast.ASTHelperFactory; 
import lapsnake.ui.JTreeLabel;

import java.awt.Component;
import java.beans.PropertyVetoException;

import org.python.antlr.ast.*;

/**Wrapper class with helper functions for With nodes*/
public class LapWith extends LapStatement {

	public final SingleChild<LapExpression> contextExpr = new SingleChild<>(this, LapExpression.class);
	public final OptionalChild<LapExpression> optionalVar = new OptionalChild<>(this, LapExpression.class);
	@NewRow public final ChildList<LapStatement> body = new ChildList<>(this, LapStatement.class, false);
	
	public LapWith(PyAST pyAST, ChildField<? super LapWith> parent, With node) throws PropertyVetoException {
		super(pyAST, parent, node);
		
		ASTHelperFactory.makeHelper(pyAST, contextExpr, node.getInternalContext_expr());
		ASTHelperFactory.makeHelper(pyAST, optionalVar, node.getInternalOptional_vars());
		ASTHelperFactory.makeHelpers(pyAST, body, node.getInternalBody());
		
		buildTextNodes(node);
	}

    
    @Override
    protected Component getFillerComponent(int childIndex,
    	ChildField<?> childField)
    {
    	if(childField == contextExpr)
    		return new JTreeLabel("With", false);
    	else if(childField == optionalVar)
    	{
    		if (optionalVar.isEmpty())
    			return null;
    		else
    			return new JTreeLabel("as", false);
    	}
    	else
    		return new JTreeLabel("Do:", true);
    }
}
